package com.qianfeng.controller;

import com.qianfeng.helper.RankingBook;
import com.qianfeng.service.BookService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@PropertySource("classpath:config/config.properties")
public class MainController {

    @Autowired
    private BookService bookService;

    @Autowired
    Environment environment;

    @RequestMapping(value = "/index")
    public String index(Model model) {
        /*站内书籍数据页所需数据*/
        /*电子图书的总数*/
        int sumOfBooks = bookService.queryNumberOfBooks();
        model.addAttribute("sumOfBooks", sumOfBooks);
        /*其他类型书籍的数目*/
        List<Integer> everyTypeBooks;
        everyTypeBooks = bookService.queryNumberOfSomeTypeBooks();
        model.addAttribute("sumOfTypeBooks", everyTypeBooks);
        /*上传日期*/
        String maxUploadDate = bookService.getMaxUploadDate();
        model.addAttribute("maxUploadDate", maxUploadDate);

        /*最新上传页数据*/
        List<RankingBook> rankingBooks;
        rankingBooks = bookService.queryByUploadedDate();
        model.addAttribute("rankingBooks", rankingBooks);
        /*热门下载页数据*/
        List<RankingBook> rankingBooks1;
        rankingBooks1 = bookService.queryByDownloadTimes();
        model.addAttribute("rankingBooks1", rankingBooks1);

        return "main";
    }
}
